import { t } from '@/locales/i18n';

export enum FlowNodeTemplateTypeEnum {
  systemInput = 'systemInput',
  ai = 'ai',
  function = 'function',
  tools = 'tools',
  interactive = 'interactive',

  search = 'search',
  multimodal = 'multimodal',
  communication = 'communication',

  other = 'other',
  teamApp = 'teamApp',

  pluginModule = 'pluginModule',
}

export enum WorkflowIOValueTypeEnum {
  string = 'string',
  number = 'number',
  boolean = 'boolean',
  object = 'object',
  arrayString = 'arrayString',
  arrayNumber = 'arrayNumber',
  arrayBoolean = 'arrayBoolean',
  arrayObject = 'arrayObject',
  arrayAny = 'arrayAny',
  any = 'any',

  chatHistory = 'chatHistory',
  datasetQuote = 'datasetQuote',

  dynamic = 'dynamic',

  // plugin special type
  selectApp = 'selectApp',
  selectDataset = 'selectDataset',

  date = 'date',
  time = 'time',
  datetime = 'datetime',
}

export const toolValueTypeList = [
  {
    label: WorkflowIOValueTypeEnum.string,
    value: WorkflowIOValueTypeEnum.string,
    jsonSchema: {
      type: 'string',
    },
  },
  {
    label: WorkflowIOValueTypeEnum.number,
    value: WorkflowIOValueTypeEnum.number,
    jsonSchema: {
      type: 'number',
    },
  },
  {
    label: WorkflowIOValueTypeEnum.boolean,
    value: WorkflowIOValueTypeEnum.boolean,
    jsonSchema: {
      type: 'boolean',
    },
  },
  {
    label: 'array<string>',
    value: WorkflowIOValueTypeEnum.arrayString,
    jsonSchema: {
      type: 'array',
      items: {
        type: 'string',
      },
    },
  },
  {
    label: 'array<number>',
    value: WorkflowIOValueTypeEnum.arrayNumber,
    jsonSchema: {
      type: 'array',
      items: {
        type: 'number',
      },
    },
  },
  {
    label: 'array<boolean>',
    value: WorkflowIOValueTypeEnum.arrayBoolean,
    jsonSchema: {
      type: 'array',
      items: {
        type: 'boolean',
      },
    },
  },
];

/* reg: modulename key */
export enum NodeInputKeyEnum {
  // old
  welcomeText = 'welcomeText',
  switch = 'switch', // a trigger switch
  history = 'history',
  answerText = 'text',

  // system config
  questionGuide = 'questionGuide',
  tts = 'tts',
  whisper = 'whisper',
  variables = 'variables',
  scheduleTrigger = 'scheduleTrigger',
  chatInputGuide = 'chatInputGuide',

  // plugin config
  instruction = 'instruction',

  // entry
  userChatInput = 'userChatInput',
  userChatInput2 = 'userChatInput2',
  inputFiles = 'inputFiles',

  agents = 'agents', // cq agent key

  // latest
  // common
  aiModel = 'model',
  aiSystemPrompt = 'systemPrompt',
  description = 'description',
  anyInput = 'system_anyInput',
  textareaInput = 'system_textareaInput',
  addInputParam = 'system_addInputParam',
  addInputParam2 = 'system_addInputParam2',
  forbidStream = 'system_forbid_stream',

  // history
  historyMaxAmount = 'maxContext',

  // ai chat
  aiChatTemperature = 'temperature',
  aiChatMaxToken = 'maxToken',
  aiChatSettingModal = 'aiSettings',
  aiChatIsResponseText = 'isResponseAnswerText',
  aiChatEnableReasoning = 'enableReasoning',
  aiChatQuoteRole = 'aiChatQuoteRole',
  aiChatQuoteTemplate = 'quoteTemplate',
  aiChatQuotePrompt = 'quotePrompt',
  aiChatDatasetQuote = 'quoteQA',
  aiChatVision = 'aiChatVision',
  stringQuoteText = 'stringQuoteText',

  // dataset
  datasetSelectList = 'datasets',
  datasetSimilarity = 'similarity',
  datasetMaxTokens = 'limit',
  datasetSearchMode = 'searchMode',
  datasetSearchUsingReRank = 'usingReRank',
  datasetSearchUsingExtensionQuery = 'datasetSearchUsingExtensionQuery',
  datasetSearchExtensionModel = 'datasetSearchExtensionModel',
  datasetSearchExtensionBg = 'datasetSearchExtensionBg',
  collectionFilterMatch = 'collectionFilterMatch',
  datasetSearchUsingKnowledgeGraph = 'datasetSearchUsingKnowledgeGraph',
  datasetSearchUsingChunkFilter = 'datasetSearchUsingChunkFilter',

  // concat dataset
  datasetQuoteList = 'system_datasetQuoteList',

  // context extract
  contextExtractInput = 'content',
  extractKeys = 'extractKeys',

  // http
  httpReqUrl = 'system_httpReqUrl',
  httpHeaders = 'system_httpHeader',
  httpMethod = 'system_httpMethod',
  httpParams = 'system_httpParams',
  httpJsonBody = 'system_httpJsonBody',
  httpFormBody = 'system_httpFormBody',
  httpContentType = 'system_httpContentType',
  httpTimeout = 'system_httpTimeout',
  abandon_httpUrl = 'url',

  // app
  runAppSelectApp = 'app',

  // plugin
  pluginId = 'pluginId',
  pluginStart = 'pluginStart',

  // if else
  condition = 'condition',
  ifElseList = 'ifElseList',

  // variable update
  updateList = 'updateList',

  // code
  code = 'code',
  codeType = 'codeType', // js|py

  // read files
  fileUrlList = 'fileUrlList',

  // user select
  userSelectOptions = 'userSelectOptions',

  // loop
  loopInputArray = 'loopInputArray',
  childrenNodeIdList = 'childrenNodeIdList',
  nodeWidth = 'nodeWidth',
  nodeHeight = 'nodeHeight',
  // loop start
  loopStartInput = 'loopStartInput',
  // loop end
  loopEndInput = 'loopEndInput',

  // form input
  userInputForms = 'userInputForms',

  // comment
  commentText = 'commentText',
  commentSize = 'commentSize',

  // feishu robot
  hookUrl = 'hook_url',

  // fetch url
  url = 'url',

  // database connection
  databaseType = 'databaseType',
  host = 'host',
  port = 'port',
  databaseName = 'databaseName',
  password = 'password',
  username = 'user',
  sql = 'sql',
}

export enum NodeOutputKeyEnum {
  // common
  userChatInput = 'userChatInput',
  userChatInput2 = 'userChatInput2',
  history = 'history',
  answerText = 'answerText', // module answer. the value will be show and save to history
  success = 'success',
  failed = 'failed',
  error = 'error',
  text = 'system_text',
  addOutputParam = 'system_addOutputParam',
  rawResponse = 'system_rawResponse',

  // start
  userFiles = 'userFiles',

  // dataset
  datasetQuoteQA = 'quoteQA',

  // classify
  cqResult = 'cqResult',
  // context extract
  contextExtractFields = 'fields',

  // tf switch
  resultTrue = 'system_resultTrue',
  resultFalse = 'system_resultFalse',

  // tools
  selectedTools = 'selectedTools',

  // http
  httpRawResponse = 'httpRawResponse',

  // plugin
  pluginStart = 'pluginStart',

  // if else
  ifElseResult = 'ifElseResult',

  // user select
  selectResult = 'selectResult',

  // loop
  loopArray = 'loopArray',

  // loop start
  loopStartInput = 'loopStartInput',

  // form input
  formInputResult = 'formInputResult',

  // time
  time = 'time',

  result = 'result',
}

export enum VariableInputEnum {
  input = 'input',
  textarea = 'textarea',
  numberInput = 'numberInput',
  select = 'select',
  cascader = 'cascader',
  switch = 'switch',
  time = 'time',
  custom = 'custom',
}
export const variableMap: Record<
  VariableInputEnum,
  {
    icon: string;
    label: string;
    value: VariableInputEnum;
    defaultValueType: WorkflowIOValueTypeEnum;
    description?: string;
  }
> = {
  [VariableInputEnum.input]: {
    icon: 'input',
    label: t('core.workflow.inputType.textInput'),
    value: VariableInputEnum.input,
    defaultValueType: WorkflowIOValueTypeEnum.string,
  },
  [VariableInputEnum.textarea]: {
    icon: 'textarea',
    label: t('core.workflow.inputType.textarea'),
    value: VariableInputEnum.textarea,
    defaultValueType: WorkflowIOValueTypeEnum.string,
    description: t('variable.textarea_type_desc'),
  },
  [VariableInputEnum.numberInput]: {
    icon: 'numberInput',
    label: t('core.workflow.inputType.number input'),
    value: VariableInputEnum.numberInput,
    defaultValueType: WorkflowIOValueTypeEnum.number,
  },
  [VariableInputEnum.select]: {
    icon: 'option',
    label: t('core.workflow.inputType.select'),
    value: VariableInputEnum.select,
    defaultValueType: WorkflowIOValueTypeEnum.string,
  },
  [VariableInputEnum.cascader]: {
    icon: 'option',
    label: t('core.workflow.inputType.tree'),
    value: VariableInputEnum.cascader,
    defaultValueType: WorkflowIOValueTypeEnum.arrayString,
  },
  [VariableInputEnum.switch]: {
    icon: 'switch',
    label: t('core.workflow.inputType.switch'),
    value: VariableInputEnum.switch,
    defaultValueType: WorkflowIOValueTypeEnum.boolean,
  },
  [VariableInputEnum.time]: {
    icon: 'time',
    label: t('core.workflow.inputType.timeSelect'),
    value: VariableInputEnum.time,
    defaultValueType: WorkflowIOValueTypeEnum.time,
  },
  [VariableInputEnum.custom]: {
    icon: 'custom',
    label: t('core.workflow.inputType.custom'),
    value: VariableInputEnum.custom,
    defaultValueType: WorkflowIOValueTypeEnum.string,
    description: t('variable.select type_desc'),
  },
};

/* run time */
export enum RuntimeEdgeStatusEnum {
  'waiting' = 'waiting',
  'active' = 'active',
  'skipped' = 'skipped',
}

export const VARIABLE_NODE_ID = 'VARIABLE_NODE_ID';
export const DYNAMIC_INPUT_REFERENCE_KEY = 'DYNAMIC_INPUT_REFERENCE_KEY';

// http node body content type
export enum ContentTypes {
  none = 'none',
  formData = 'form-data',
  xWwwFormUrlencoded = 'x-www-form-urlencoded',
  json = 'json',
  xml = 'xml',
  raw = 'raw-text',
}

export enum FlowNodeInputTypeEnum { // render ui
  reference = 'reference', // reference to other node output
  input = 'input', // one line input
  textarea = 'textarea',
  numberInput = 'numberInput',
  switch = 'switch', // true/false
  select = 'select',

  // editor
  JSONEditor = 'JSONEditor',

  addInputParam = 'addInputParam', // params input

  // special input
  selectApp = 'selectApp',
  customVariable = 'customVariable',

  // ai model select
  selectLLMModel = 'selectLLMModel',
  settingLLMModel = 'settingLLMModel',

  // dataset special input
  selectDataset = 'selectDataset',
  selectDatasetParamsModal = 'selectDatasetParamsModal',
  settingDatasetQuotePrompt = 'settingDatasetQuotePrompt',

  hidden = 'hidden',
  custom = 'custom',

  // 级联选择
  cascader = 'cascader',

  // 时间选择
  time = 'time',
}

export const FlowNodeInputMap: Record<
  FlowNodeInputTypeEnum,
  {
    icon: string;
  }
> = {
  [FlowNodeInputTypeEnum.reference]: {
    icon: 'reference',
  },
  [FlowNodeInputTypeEnum.numberInput]: {
    icon: 'number',
  },
  [FlowNodeInputTypeEnum.select]: {
    icon: 'option',
  },
  [FlowNodeInputTypeEnum.switch]: {
    icon: 'switch',
  },
  [FlowNodeInputTypeEnum.JSONEditor]: {
    icon: 'json',
  },
  [FlowNodeInputTypeEnum.addInputParam]: {
    icon: 'dynamic',
  },
  [FlowNodeInputTypeEnum.selectApp]: {
    icon: 'select-app',
  },
  [FlowNodeInputTypeEnum.selectLLMModel]: {
    icon: 'damoxing',
  },
  [FlowNodeInputTypeEnum.settingLLMModel]: {
    icon: 'damoxing',
  },
  [FlowNodeInputTypeEnum.selectDataset]: {
    icon: 'data-set',
  },
  [FlowNodeInputTypeEnum.selectDatasetParamsModal]: {
    icon: 'data-set',
  },
  [FlowNodeInputTypeEnum.settingDatasetQuotePrompt]: {
    icon: 'data-set',
  },
  [FlowNodeInputTypeEnum.hidden]: {
    icon: 'select',
  },
  [FlowNodeInputTypeEnum.customVariable]: {
    icon: 'variable',
  },
  [FlowNodeInputTypeEnum.custom]: {
    icon: 'custom',
  },
  [FlowNodeInputTypeEnum.input]: {
    icon: 'select',
  },
  [FlowNodeInputTypeEnum.textarea]: {
    icon: 'textarea',
  },
  [FlowNodeInputTypeEnum.cascader]: {
    icon: 'select',
  },
  [FlowNodeInputTypeEnum.time]: {
    icon: 'time',
  },
};

export enum FlowNodeOutputTypeEnum {
  hidden = 'hidden',
  source = 'source',
  static = 'static',
  dynamic = 'dynamic',
}

export enum FlowNodeTypeEnum {
  emptyNode = 'emptyNode',
  systemConfig = 'userGuide',
  pluginConfig = 'pluginConfig',
  globalVariable = 'globalVariable',
  workflowStart = 'workflowStart',
  chatNode = 'chatNode',

  datasetSearchNode = 'datasetSearchNode',
  datasetConcatNode = 'datasetConcatNode',

  answerNode = 'answerNode',
  classifyQuestion = 'classifyQuestion',
  contentExtract = 'contentExtract',
  httpRequest468 = 'httpRequest468',
  runApp = 'app',
  appModule = 'appModule',
  pluginModule = 'pluginModule',
  pluginInput = 'pluginInput',
  pluginOutput = 'pluginOutput',
  queryExtension = 'cfr',
  tools = 'tools',
  stopTool = 'stopTool',
  toolParams = 'toolParams',
  lafModule = 'lafModule',
  ifElseNode = 'ifElseNode',
  variableUpdate = 'variableUpdate',
  code = 'code',
  textEditor = 'textEditor',
  customFeedback = 'customFeedback',
  readFiles = 'readFiles',
  userSelect = 'userSelect',
  loop = 'loop',
  loopStart = 'loopStart',
  loopEnd = 'loopEnd',
  formInput = 'formInput',
  comment = 'comment',
  toolSet = 'toolSet',
  tool = 'tool',

  // 插件
  getTime = 'community-getTime',
  feishuRobot = 'community-feishu',
  fetchUrl = 'community-fetchUrl',
  databaseConnection = 'community-databaseConnection',
  dingding = 'community-dingding',
  wechatWork = 'community-wechatWork',
  smtp = 'community-smtp',
}

// node IO value type
export const FlowValueTypeMap = {
  [WorkflowIOValueTypeEnum.string]: {
    label: 'string',
    value: WorkflowIOValueTypeEnum.string,
  },
  [WorkflowIOValueTypeEnum.number]: {
    label: 'number',
    value: WorkflowIOValueTypeEnum.number,
  },
  [WorkflowIOValueTypeEnum.boolean]: {
    label: 'boolean',
    value: WorkflowIOValueTypeEnum.boolean,
  },
  [WorkflowIOValueTypeEnum.object]: {
    label: 'object',
    value: WorkflowIOValueTypeEnum.object,
  },
  [WorkflowIOValueTypeEnum.arrayString]: {
    label: 'array<string>',
    value: WorkflowIOValueTypeEnum.arrayString,
  },
  [WorkflowIOValueTypeEnum.arrayNumber]: {
    label: 'array<number>',
    value: WorkflowIOValueTypeEnum.arrayNumber,
  },
  [WorkflowIOValueTypeEnum.arrayBoolean]: {
    label: 'array<boolean>',
    value: WorkflowIOValueTypeEnum.arrayBoolean,
  },
  [WorkflowIOValueTypeEnum.arrayObject]: {
    label: 'array<object>',
    value: WorkflowIOValueTypeEnum.arrayObject,
  },
  [WorkflowIOValueTypeEnum.arrayAny]: {
    label: 'array',
    value: WorkflowIOValueTypeEnum.arrayAny,
  },
  [WorkflowIOValueTypeEnum.any]: {
    label: 'any',
    value: WorkflowIOValueTypeEnum.any,
  },
  [WorkflowIOValueTypeEnum.chatHistory]: {
    label: t('core.chat.History'),
    value: WorkflowIOValueTypeEnum.chatHistory,
  },
  [WorkflowIOValueTypeEnum.datasetQuote]: {
    label: t('core.workflow.Dataset quote'),
    value: WorkflowIOValueTypeEnum.datasetQuote,
  },
  [WorkflowIOValueTypeEnum.selectApp]: {
    label: t('plugin.App'),
    value: WorkflowIOValueTypeEnum.selectApp,
  },
  [WorkflowIOValueTypeEnum.selectDataset]: {
    label: t('core.chat.Select dataset'),
    value: WorkflowIOValueTypeEnum.selectDataset,
  },
  [WorkflowIOValueTypeEnum.dynamic]: {
    label: t('core.workflow.dynamic_input'),
    value: WorkflowIOValueTypeEnum.dynamic,
  },
};

export const FlowTimeValueTypeMap = {
  [WorkflowIOValueTypeEnum.time]: {
    label: t('core.workflow.inputType.time'),
    value: WorkflowIOValueTypeEnum.time,
  },
  [WorkflowIOValueTypeEnum.date]: {
    label: t('core.workflow.inputType.date'),
    value: WorkflowIOValueTypeEnum.date,
  },
  [WorkflowIOValueTypeEnum.datetime]: {
    label: t('core.workflow.inputType.datetime'),
    value: WorkflowIOValueTypeEnum.datetime,
  },
};

export const AllFlowValueTypeMap = {
  ...FlowValueTypeMap,
  ...FlowTimeValueTypeMap,
};

export const EDGE_TYPE = 'default';
export const defaultNodeVersion = '481';

export const chatHistoryValueDesc = `{
  obj: System | Human | AI;
  value: string;
}[]`;
export const datasetQuoteValueDesc = `{
  id: string;
  datasetId: string;
  collectionId: string;
  sourceName: string;
  sourceId?: string;
  q: string;
  a: string
}[]`;

export const workflowNodeTemplateList = [
  {
    type: FlowNodeTemplateTypeEnum.systemInput,
    label: t('core.module.template.System input module'),
    list: [],
  },
  {
    type: FlowNodeTemplateTypeEnum.ai,
    label: t('core.module.template.AI function'),
    list: [],
  },
  {
    type: FlowNodeTemplateTypeEnum.search,
    label: t('core.workflow.template.Search'),
    list: [],
  },
  {
    type: FlowNodeTemplateTypeEnum.interactive,
    label: t('core.workflow.template.Interactive'),
    list: [],
  },
  {
    type: FlowNodeTemplateTypeEnum.multimodal,
    label: t('core.workflow.template.Multimodal'),
    list: [],
  },
  {
    type: FlowNodeTemplateTypeEnum.tools,
    label: t('core.module.template.Tool module'),
    list: [],
  },
  {
    type: FlowNodeTemplateTypeEnum.communication,
    label: t('workflow.template.communication'),
    list: [],
  },
  {
    type: FlowNodeTemplateTypeEnum.other,
    label: t('common.Other'),
    list: [],
  },
  {
    type: FlowNodeTemplateTypeEnum.teamApp,
    label: '',
    list: [],
  },
];

export const SnapshotLimit = 30;
